package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryEvaPortraitDetailReq;
import com.sg.dto.base.app.req.QueryEvaPortraitListReq;
import com.sg.entity.OmsEvaluationPortrait;
import com.sg.service.base.app.MOmsEvaluationPortraitService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsEvaluationPortraitMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsEvaluationPortraitServiceImpl
        extends ServiceImpl<OmsEvaluationPortraitMapper, OmsEvaluationPortrait>
        implements MOmsEvaluationPortraitService {

    @Autowired
    OmsEvaluationPortraitMapper omsEvaluationPortraitMapper;

    /**
     * 3-2-04查评价画像详情[2408]
     * gen by moon at 4/24/2023, 12:54:17 AM
     */
    @Trace(operationName = "3-2-04查评价画像详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationPortrait queryEvaPortraitDetail(QueryEvaPortraitDetailReq reqDto) {

        try {

            QueryWrapper<OmsEvaluationPortrait> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationPortrait> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationPortrait retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-04查评价画像列表[2368]
     * gen by moon at 10/15/2022, 5:44:05 PM
     */
    @Trace(operationName = "3-2-04查评价画像列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationPortrait> queryEvaPortraitList(QueryEvaPortraitListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationPortrait> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationPortrait.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationPortrait.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationPortrait.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationPortrait>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-04新增评价画像[2349]
     * gen by moon at 10/15/2022, 5:44:05 PM
     */
    @Trace(operationName = "3-2-04新增评价画像")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEvaPortrait(OmsEvaluationPortrait omsEvaluationPortrait) {
        omsEvaluationPortrait.setEvaluationPortraitId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationPortrait.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsEvaluationPortrait.setIsUpdate("TRUE");
        omsEvaluationPortrait.setUpdateStatus("NEW");
        omsEvaluationPortrait.setArchivngType("NOT_ARCHIVE");
        omsEvaluationPortrait.setIsEditing("TRUE");
        omsEvaluationPortrait.setIsArchive("FALSE");
        omsEvaluationPortrait.setSubjectLifeCycle("EDITING");
        omsEvaluationPortrait.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationPortrait.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationPortrait.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationPortrait.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationPortrait.setIsValid("FALSE");
        omsEvaluationPortrait.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationPortrait.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationPortrait)) {
            return omsEvaluationPortrait.getEvaluationPortraitId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-04删除评价画像[4209]
     * gen by moon at 10/15/2022, 5:44:07 PM
     */
    @Trace(operationName = "3-2-04删除评价画像")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEvaPortrait(String evaluationPortraitId) {

        return super.removeById(evaluationPortraitId);
    }

    /**
     * 3-2-04修改评价画像[7171]
     * gen by moon at 6/9/2023, 8:40:45 PM
     */
    @Trace(operationName = "3-2-04修改评价画像")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEvaPortrait(OmsEvaluationPortrait omsEvaluationPortrait) {

        return super.updateById(omsEvaluationPortrait);
    }
}
